package iscript.gameFramework.IsoLibarys.math 
{
	import flash.geom.Rectangle;
	import iscript.coreFramework.data.datatable.DataTable;
	import iscript.coreFramework.math.Point2D;
	import iscript.coreFramework.math.Vector2D;
	import iscript.gameFramework.IsoLibarys.data.IsoBound;
	import iscript.gameFramework.IsoLibarys.data.IsoBaseData;
	import iscript.gameFramework.IsoLibarys.IsoScene;
	import iscript.gameFramework.IsoLibarys.IsoView;
	import iscript.coreFramework.reflect.getClassName;
	/**
	 * ...
	 * @author raymond
	 */
	public class IsoMath 
	{
		
		public function IsoMath() 
		{
			
		}
		
		static public const  scaleX:Number = 1.42;		
		static public const  scaleY:Number = 0.71;
		 /**
		 * 是否包含单个或范围格子
		 * @param	v
		 * @return
		 */
		static public function viewContainBound(view:IsoView, data:Object):Boolean { 
			  var _result:Boolean = true;			  
			  var scene:IsoScene = view.getIsoScene();
			  switch(getClassName(data)) {
				    case  "Point2D":
						var v:Point2D = data as Point2D;
						if (v.x<0 || v.x>=scene.gridSize) _result = false; 
						if (v.y<0 || v.y>=scene.gridSize) _result = false; 
						break;
				    case "IsoBound":
						 data = data as IsoBound;
				        if (data.topGrid.x<0 || data.topGrid.x>=scene.gridSize) _result = false; 
					    if (data.topGrid.y<0 || data.topGrid.y>=scene.gridSize) _result = false; 
					    if (data.bottomGrid.x<0 || data.bottomGrid.x>=scene.gridSize) _result = false; 
					    if (data.bottomGrid.y<0 || data.bottomGrid.y>=scene.gridSize) _result = false; 
					break;
				    default:
					_result = false;
					break;
				}
			  return _result;
			}
		
		/***
		 *  根据单个格子获得Rectangle
		 * */
		static public function getGridRectangle(view:IsoView, v:Point2D):Rectangle {
			if(!viewContainBound(view,v)) return null;	
			var scene:IsoScene = view.getIsoScene();
			var gridData:DataTable = scene.getGridData();
			if (gridData.RowCount == 0) return null;			
			return  IsoBaseData(gridData.getCell(v.x, v.y.toString())).Bound;			   
			}
		 
		/**
		 * 根据格子范围获得Rectangle
		 * @param	data
		 * @return
		 */
		static public function getIsoBoundRectangle(view:IsoView, data:IsoBound):Rectangle {
			if(!viewContainBound(view,data)) return null;
			var scene:IsoScene = view.getIsoScene();
			 return  new Rectangle(
								  getGridRectangle(view,data.leftGrid).x,
			                      getGridRectangle(view,data.topGrid).y,
								  getGridRectangle(view,data.rightGrid).x - getGridRectangle(view,data.leftGrid).x + scene.graphicsGridWidth,
			                      getGridRectangle(view,data.bottomGrid).y - getGridRectangle(view,data.topGrid).y + scene.graphicsGridHeight
			                     );
			}
			
		/*static public function getLocalBoundRectangle(view:IsoView, data:IsoBound):Rectangle {

			 var LGridBound:Rectangle = getGridRectangle(view,data.leftGrid);
			 var TGridBound:Rectangle =  getGridRectangle(view,data.topGrid);			 
			 var BGirdBound:Rectangle =  getGridRectangle(view,data.bottomGrid);
			 var RGirdBound:Rectangle =  getGridRectangle(view,data.rightGrid);	
			 
			 var _rect:Rectangle=  new Rectangle(  LGridBound.x ,
													 TGridBound.y ,
													 RGirdBound.x - LGridBound.x+ RGirdBound.width,
													 BGirdBound.y -  TGridBound.y+ BGirdBound.height 
												   );
								   return _rect;				 
								 
								 
			}*/
			
			
		/**
		 * 获得单个Grid中图形(菱形)顶点 [上 左 下 右]
		 * @param	v
		 * @return
		 */
		static public function getGridDiamondVertex(view:IsoView, v:Point2D):Vector.<Point2D> {
				var _pos:Vector.<Point2D> = new Vector.<Point2D>();
				if (!viewContainBound(view, v)) return _pos;
			    var scene:IsoScene = view.getIsoScene();
				if (scene.getGridData().RowCount == 0) return null;			
				var _rect:Rectangle = getGridRectangle(view,v);
				_pos.push(new Point2D(_rect.width/2+_rect.x,0+_rect.y));
				_pos.push(new Point2D(0+_rect.x,_rect.height/2+_rect.y));
				_pos.push(new Point2D(_rect.width/2+_rect.x,_rect.height+_rect.y));
				_pos.push(new Point2D(_rect.width+_rect.x,_rect.height/2+_rect.y));
				return  _pos; 
			}
		 
		/**
		 * 获得范围格子组成的图形(菱形)顶点 [上 左 下 右]
		 * @param	boundgrid
		 * @return
		 */	
		static public function getIsoBoundDiamondVertex(view:IsoView, boundgrid:IsoBound):Vector.<Point2D> {
			    var  _position:Vector.<Point2D> = new Vector.<Point2D>();	
				if (!viewContainBound(view, boundgrid)) return _position;
				
				var  _top:Vector.<Point2D> = getGridDiamondVertex(view,boundgrid.topGrid);
				var  _left:Vector.<Point2D> = getGridDiamondVertex(view,boundgrid.leftGrid);
				var  _bottom:Vector.<Point2D> = getGridDiamondVertex(view,boundgrid.bottomGrid);
				var  _right:Vector.<Point2D> = getGridDiamondVertex(view,boundgrid.rightGrid);
				    _position.push(new Point2D(_top[0].x,_top[0].y));
					_position.push(new Point2D(_left[1].x,_left[1].y));
					_position.push(new Point2D(_bottom[2].x,_bottom[2].y));
					_position.push(new Point2D(_right[3].x,_right[3].y));
					
				return _position;
			 }
			 
			 
		/**
		 * 上 左 下 右
		 * @param	boundgrid
		 * @return
		 */
		static public function getLocalBoundDiamondVertex(view:IsoView, boundgrid:IsoBound):Vector.<Point2D> {
			    var  _position:Vector.<Point2D> = new Vector.<Point2D>();
				
				var  _top:Vector.<Point2D> = getGridDiamondVertex(view,boundgrid.topGrid);
				var  _left:Vector.<Point2D> = getGridDiamondVertex(view,boundgrid.leftGrid);
				var  _bottom:Vector.<Point2D> = getGridDiamondVertex(view,boundgrid.bottomGrid);
				var  _right:Vector.<Point2D> = getGridDiamondVertex(view, boundgrid.rightGrid);				
				if ( _top == null || _left == null || _bottom == null || _right == null) return null;
				
				     _position.push(new Point2D(_top[0].x-_left[1].x,_top[0].y-_top[0].y));
					_position.push(new Point2D(_left[1].x-_left[1].x,_left[1].y-_top[0].y));
					_position.push(new Point2D(_bottom[2].x-_left[1].x,_bottom[2].y-_top[0].y));
					_position.push(new Point2D(_right[3].x - _left[1].x, _right[3].y - _top[0].y));				
				return _position;
			 }	 
		
		static public function getLocalBoundRectangle(view:IsoView, boundgrid:IsoBound):Rectangle {			
				var  _top:Vector.<Point2D> = getGridDiamondVertex(view,boundgrid.topGrid);
				var  _left:Vector.<Point2D> = getGridDiamondVertex(view,boundgrid.leftGrid);
				var  _bottom:Vector.<Point2D> = getGridDiamondVertex(view,boundgrid.bottomGrid);
				var  _right:Vector.<Point2D> = getGridDiamondVertex(view, boundgrid.rightGrid);				
				var  _r:Rectangle = new Rectangle(0,0, _right[3].x-_left[1].x, _bottom[2].y- _top[0].y)
				return _r;
			 }
	         
			 
		 static public function getVector2DByBound(view:IsoView,bound:IsoBound):Vector2D {
			     var leftPoint:Point2D = getGridDiamondVertex(view, bound.leftGrid)[1];
				 var rightPoint:Point2D = getGridDiamondVertex(view, bound.rightGrid)[3];
				 return new Vector2D(leftPoint, rightPoint);
			 } 	 
			 
			 
			 
			 
		/**
		 * 检测范围是开放的
		 * @param	view
		 * @param	bound
		 * @return
		 */
		 static public function checkIsoBoundOpen(view:IsoView, bound:IsoBound):Boolean {
			       var _data:Vector.<IsoBaseData> = isoBaseDataByBound(view, bound);
				   for each( var item:IsoBaseData in  _data) {
					     if (item.IsBuilder) return  false;
					   }
					return true;
			 }  		 
		
			 
		/**
		 * 获得范围内的所有IsoBaseData数据
		 * @param	view
		 * @param	bound
		 * @return
		 */	 
		static public function isoBaseDataByBound(view:IsoView, bound:IsoBound):Vector.<IsoBaseData> {
			  var _data:Vector.<IsoBaseData> = new Vector.<IsoBaseData>();
			   for ( var i:int = bound.topGrid.x; i <= bound.bottomGrid.x; i++) {
				   for ( var j:int = bound.topGrid.y; j <= bound.bottomGrid.y; j++) {
				        _data.push(view.Data.getCell(i, j.toString()));
				   }
			   }
			 return _data;
			}
			 
			 
			 
		/*	 static public   getBound(isoscene:IsoScene = null):Rectangle {
			if (isoscene != null )_isoscene = isoscene;
			if ( _isoscene ==null || _isoscene.gridSize == 0) return  null;	
			return null;
			 var grids:DataTable = _isoscene.getGrids();			 
			 var LGridBound:Rectangle = _isoscene.getGridRectangle(leftGrid);
			 var TGridBound:Rectangle =  _isoscene.getGridRectangle(topGrid);			 
			 var BGirdBound:Rectangle =  _isoscene.getGridRectangle(bottomGrid);
			 var RGirdBound:Rectangle =  _isoscene.getGridRectangle(rightGrid);	
			 

			 
			 var _rect:Rectangle=  new Rectangle(  LGridBound.x ,
													 TGridBound.y ,
													 RGirdBound.x - LGridBound.x+ RGirdBound.width,
													 BGirdBound.y -  TGridBound.y+ BGirdBound.height 
												   );
								   return _rect;
			}*/
			 
			 
			 
	}

}